package com.marry.multidatasource.controller;

import com.github.pagehelper.PageInfo;
import com.marry.multidatasource.model.RequestItem;
import com.marry.multidatasource.model.fg.RoomRight;
import com.marry.multidatasource.service.ZownerService;
import com.marry.multidatasource.util.Cardhelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ml
 * @create 2018-01-10--15:35
 */
@RestController
public class ZownerController {
    @Autowired
    private ZownerService zownerService;

    /**
     * 根据姓名查询
     * @param requestItem
     * @return
     */
    @GetMapping("/getRoomsByName")
    public Map<String,Object> getRightsByName(RequestItem requestItem){
        System.out.println(requestItem);
        Map<String,Object> map=new HashMap<String,Object>(2);

        List<RoomRight> rights=zownerService.getRoomListByName(requestItem);
        PageInfo<RoomRight> pageInfo=new PageInfo(rights);
        System.out.println("pageinfo.count"+pageInfo.getTotal());
        System.out.println("pageinfo.pages"+pageInfo.getPages());
        map.put("data",rights);
        map.put("pages",pageInfo.getPages());
        return map;
    }

    /**
     * 根据坐落或者产权证号查询
     * @param requestItem
     * @return
     */
    @GetMapping("/getRoomsByLocationOrCqzh")
    public Map<String,Object> getRightsByLocationOrCqzh(RequestItem requestItem){
        System.out.println("requestItem = [" + requestItem + "]");
        Map<String,Object> map=new HashMap<String, Object>(2);

        List<RoomRight> roomRights=zownerService.getRoomListByLocationOrCqzh(requestItem);
        PageInfo<RoomRight> pageInfo=new PageInfo<RoomRight>(roomRights);

        System.out.println(pageInfo.getTotal());
        map.put("data",roomRights);
        map.put("pages",pageInfo.getPages());

        return map;
    }

    @RequestMapping("/getRoomsByCertno")
    public Map<String,Object> getRightsByCertno(RequestItem requestItem){
        System.out.println("ZownerController.getRightsByCertno");
        System.out.println("requestItem = [" + requestItem + "]");
        Map<String,Object> map=new HashMap<String, Object>(2);
        if(requestItem.getCertno()!=null){

            String certno=requestItem.getCertno();
            List<String> certnos=new ArrayList<String>();
            addCertno(certnos,certno,requestItem);
            if(certnos.size()>1){
                requestItem.setCertnos(certnos);
                requestItem.setCertno(null);
            }
        }else if(requestItem.getCertnos()!=null){
            List<String> certnos=new ArrayList<String>();
            for(String certno:requestItem.getCertnos()){

                addCertno(certnos,certno,requestItem);

            }
            if(certnos.size()>1){
                requestItem.setCertnos(certnos);
                requestItem.setCertno(null);
            }
        }
        List<RoomRight> roomRightList=zownerService.getRoomsByCertno(requestItem);
        PageInfo pageInfo=new PageInfo(roomRightList);
        System.out.println("count = " + pageInfo.getTotal());
        map.put("data",roomRightList);
        map.put("pages",pageInfo.getPages());

        return map;


    }

    private void addCertno(List<String> certnos, String certno, RequestItem requestItem) {
        if(Cardhelper.isIdentityId(certno.toUpperCase())){
            if(!certno.equals(certno.toLowerCase()) || !certno.equals(certno.toUpperCase())){
//                    包含X
                certnos.add(certno.toUpperCase());
                certnos.add(certno.toLowerCase());

            }else{
                certnos.add(certno);
            }
            if(requestItem.getIs15()!=null){
                certnos.add(Cardhelper.newCodeToOld(certno));
            }


        }
    }
}
